package com.sebiotec.manager.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;

import com.allen.library.RxHttpUtils;
import com.sebiotec.basecommon.utils.base.BaseActivity;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.BookNameUtils;
import com.sebiotec.basecommon.utils.tools.SPUtils;

import com.sebiotec.manager.R;
import com.sebiotec.manager.R2;
import com.sebiotec.manager.model.event.BasicEvent;
import com.sebiotec.manager.model.response.UseProfileResponse;
import com.sebiotec.manager.viewmodel.BasicViewModel;
import com.xuexiang.xui.widget.toast.XToast;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class BasicActivity extends BaseActivity {


    @BindView(R2.id.qk_back)
    ImageView qkBack;
    @BindView(R2.id.tv_save)
    TextView tvSave;
    @BindView(R2.id.et_ename)
    EditText etEname;
    @BindView(R2.id.et_cname)
    EditText etCname;
    @BindView(R2.id.et_phone)
    EditText etPhone;
    @BindView(R2.id.et_email)
    EditText etEmail;
    @BindView(R2.id.et_roles)
    EditText etRole;
    @BindView(R2.id.et_group)
    EditText etUserGroup;
    @BindView(R2.id.et_nick)
    EditText etNick;
    private BasicViewModel mBasicViewModel;

    @Override
    protected int setLayoutId() {
        return R.layout.man_activity_basic;
    }

    @Override
    protected void initUI() {
        mBasicViewModel = ViewModelProviders.of(this).get(BasicViewModel.class);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxHttpUtils.cancel("ManagerBasic");
    }
    @Override
    protected void initData() {
        mBasicViewModel.getUser(new BasicViewModel.MyBasicDataCallBack<Object>() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                UseProfileResponse bean = (UseProfileResponse) o;
                etEname.setText(bean.getEName());
                etCname.setText(bean.getCName());
                etPhone.setText(bean.getPhoneNo());
                etEmail.setText(bean.getEmail());
                etRole.setText(BookNameUtils.getName(bean.getRoleId()));
                etUserGroup.setText(bean.getUserGroup() + "");
                etNick.setText(bean.getNickName());
            }

            @Override
            public void onDataFailCallBack(Object o) {

            }
        });
    }

    @Override
    protected void setListener() {

    }


    @OnClick({R2.id.qk_back, R2.id.tv_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.qk_back) {
            ActivityTool.getInstance().popOneActivity(this);
        } else if (id == R.id.tv_save) {
            if (TextUtils.isEmpty(etCname.getText().toString()) || TextUtils.isEmpty(etEname.getText().toString())) {
                XToast.error(this, getString(R.string.ecname_not_null)).show();
                return;
            } else {
                UseProfileResponse bean = new UseProfileResponse();
                bean.setCName(etCname.getText().toString());
                bean.setEmail(etEmail.getText().toString());
                bean.setEName(etEname.getText().toString());
                bean.setPhoneNo(etPhone.getText().toString());
                bean.setRoleId(Integer.parseInt(etRole.getText().toString()));
                bean.setUserGroup(etUserGroup.getText().toString());
                bean.setLoginId(SPUtils.get(SjmConstant.LOGINID, ""));
                mBasicViewModel.save(bean, new BasicViewModel.MyBasicDataCallBack<Object>() {
                    @Override
                    public void onDataSuccessCallBack(Object o) {
                        XToast.success(BasicActivity.this, getString(R.string.update_basic_success)).show();
                        EventBus.getDefault().post(new BasicEvent(etEname.getText().toString()));
                        initData();
                    }

                    @Override
                    public void onDataFailCallBack(Object o) {
                        XToast.error(BasicActivity.this, getString(R.string.bc_failed)).show();
                    }
                });
            }

        }
    }


}
